package com.mediacloud.app.newsmodule.fragment

import android.app.Activity
import android.content.Context
import android.util.Log
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import java.util.concurrent.atomic.AtomicBoolean

abstract class ListPreLoadKitBAK<T>(val context: Context, val refreshLayout: SmartRefreshLayout) {
    companion object {

        var LTAG = "LIST_PRELOAD"
    }

    var preLoadData: MutableList<T> = ArrayList()
    var loadMore = AtomicBoolean(false)
    var pageNum = 1
    var pendingNum = -1
    var hasMore = false
    var preNum = -1

    fun onRefresh() {
        refreshLayout.setEnableLoadMore(false)
        hasMore = false
        pageNum = 1
        loadMore.set(false)
        preLoadData.clear()
    }

    fun onLoadMore() {
        Log.w(LTAG, "滚动到了底部  loadMore=$loadMore")
        if (!loadMore.get()) {
            Log.w(LTAG, "标记 loadMore = true")
            loadMore.set(true)
            pendingNum = -1
            if (context is Activity) {
                if (context.isDestroyed || context.isFinishing) {
                    return
                }
            }
            if (preLoadData.size > pageNum) {
                val preLoad = preLoadData[pageNum]
                pageNum++
                Log.w(LTAG, "R2Beat 已有缓存 直接加载第【${pageNum}】页的数据")
                onSetData(preLoad)
                //同时去加载下一页的
                preload()
                loadMore.set(false)
                Log.w(LTAG, "标记 loadMore1 = false")
            } else {
                if (hasMore) {
                    pendingNum = pageNum + 1
                    Log.w(LTAG, "R2Beat 没有缓存 等待接口 触发加载更改动画  等待加载【${pendingNum}页的数据】")
                    refreshLayout.setEnableLoadMore(true)
                    refreshLayout.autoLoadMore(0, 400, 1.0f, true)
                    getMoreDate()
                } else {
                    noMoreData()
                }
            }

        }
    }

    fun getMoreDate() {
        //  refreshLayout.setEnableLoadMore(false)
        if (preNum < pendingNum) {
            //加载数据 等待结果
            preload()
        } else {
            //挂起等待结果
        }
    }


    private fun noMoreData() {
        Log.w(LTAG, "没有更多数据")
        refreshLayout.setEnableLoadMore(true)
        refreshLayout.finishLoadMoreWithNoMoreData()
        loadMore.set(false)
    }

    abstract fun onSetData(data: T)
    abstract fun onPreload(preNum: Int)


    fun preload() {
        if (hasMore) {
            preNum = preLoadData.size + 1
            Log.w(ListPreLoadKit.LTAG, "开始预加载 $preNum")
            onPreload(preNum)
        } else {
            noMoreData()
        }
    }

    fun onPreloadSuccess(data: T, preNum: Int) {
        if (context is Activity) {
            if (context.isDestroyed || context.isFinishing) {
                return
            }
        }
        preLoadData.add(data)
        Log.w(LTAG, "添加预加载数据 preNum = $preNum")
        if (preLoadData.size == pendingNum) {

            Log.w(LTAG, "R2Beat 接口返回${pendingNum}页数据，设置数据")
            onSetData(preLoadData[pageNum])
            loadMore.set(false)
            refreshLayout.finishLoadMore()
            refreshLayout.setEnableLoadMore(false)
            pageNum++
            Log.w(LTAG, "标记 loadMore2 = false")
            pendingNum = -1
            preload()

        }
    }

    fun onPreloadFail(preNum: Int) {
        Log.w(LTAG, "预加载${preNum}页数据!!失败了!! preNum-1")
        this.preNum--
        if (pendingNum == preNum) {
            loadMore.set(false)
            Log.w(LTAG, "标记 loadMore1 = false")
            refreshLayout.finishLoadMore(false)
            refreshLayout.setEnableLoadMore(false)
        }
    }
}